package com.yy.controller.admin;

import com.yy.controller.service.CategoryService;
import com.yy.dto.CategoryQueryDTO;
import com.yy.entity.Category;
import com.yy.result.PageResult;
import com.yy.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
@Api(tags = "分类管理接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分类分页条件查询
     *
     * @param categoryQueryDTO
     * @return
     */
    @GetMapping("/list")
    @CrossOrigin
    @ApiOperation("分类分页条件查询")
    public Result pageList(CategoryQueryDTO categoryQueryDTO) {
        PageResult pageResult = categoryService.pageList(categoryQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @CrossOrigin
    @ApiOperation("删除分类")
    public Result delete(@PathVariable("id") Long id) {
        categoryService.delete(id);
        return Result.success();
    }

    /**
     * 添加分类
     *
     * @param category
     * @return
     */
    @PostMapping("/add")
    @CrossOrigin
    @ApiOperation("新增分类")
    public Result add(@RequestBody Category category) {
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 修改分类
     *
     * @param category
     * @return
     */
    @PutMapping("/update")
    @CrossOrigin
    @ApiOperation("修改分类")
    public Result update(@RequestBody Category category) {
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 根据id修改分类状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("editStatus/{id}/{status}")
    @CrossOrigin
    @ApiOperation("修改状态")
    public Result editStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        categoryService.editstatus(id, status);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/listByType/{type}")
    @CrossOrigin
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> listByType(@PathVariable("type") Integer type) {
       List<Category> categoryList  =categoryService.listByType(type);
       return Result.success(categoryList);
    }
}
